package net.huashitong.interview.controller;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.Indicators.utils.DateForIndUtils;
import net.huashitong.interview.entity.EnterpriseFuWu;
import net.huashitong.interview.filter.EnterpriseFuWuFilter;
import net.huashitong.interview.service.EnterpriseFuWuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/EnterpriseFuWu")
public class EnterpriseFuWuController extends JsonBaseController {

    @Autowired
    private EnterpriseFuWuService service;
    @Override
    protected String getView(String s) {
        return null;
    }

    /**
     * 企业服务月    首页
     * @param filter    （fillDate【201807】）
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getindex",method = RequestMethod.POST)
    public Message getIndex(@RequestBody EnterpriseFuWuFilter filter){

        String fillDate = "";
        if(ObjectUtils.isBlank(filter.getFillDate())){
            //获取数据库中最新的时间
            fillDate = service.getNewFillDate();
            if("0000".equals(fillDate)){
                return coverMessage("200", "暂无数据");
            }
            filter.setFillDate(fillDate.substring(0,6));
        }else{
            fillDate = filter.getFillDate();
        }
        List<EnterpriseFuWu> list = service.queryFuWu(filter);
        if(list.size()==0){
            return coverMessage("200", "暂无数据");
        }
        List<Map<String, Object>> result = service.baseResult(list);
        Map<String, Object> map2 = new HashMap<>();
        map2.put("fillDate",fillDate);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("list",result);
        resultMap.put("time",map2);
        return coverMessage("200", "获取成功", resultMap);
    }

    /**
     * 企业服务月    详细页（具体问题）
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getdetailbyid",method = RequestMethod.POST)
    public Message getDetailById(@RequestBody EnterpriseFuWuFilter filter){
        EnterpriseFuWu enterpriseFuWu = service.getById(filter.getSysId());
        if(ObjectUtils.isBlank(enterpriseFuWu)){
            return coverMessage("200", "无详细信息");
        }
        Map<String,String> map = new HashMap<>();

        map.put("sysId",filter.getSysId());
        map.put("helpEnterprise",ObjectUtils.isNotBlank(enterpriseFuWu.getHelpEnterprise())?enterpriseFuWu.getHelpEnterprise():"-");
        map.put("fillDate",ObjectUtils.isNotBlank(enterpriseFuWu.getFillDate())?enterpriseFuWu.getFillDate():"-");
        map.put("fillUnitId",ObjectUtils.isNotBlank(enterpriseFuWu.getFillUnitId())?enterpriseFuWu.getFillUnitId():"-");
        map.put("specificProblem",ObjectUtils.isNotBlank(enterpriseFuWu.getSpecificProblem())?enterpriseFuWu.getSpecificProblem():"-");
        map.put("solveSchedule",ObjectUtils.isNotBlank(enterpriseFuWu.getSolveSchedule())?enterpriseFuWu.getSolveSchedule():"-");
        map.put("nextStep",ObjectUtils.isNotBlank(enterpriseFuWu.getNextStep())?enterpriseFuWu.getNextStep():"-");
        map.put("remark",ObjectUtils.isNotBlank(enterpriseFuWu.getRemark())?enterpriseFuWu.getRemark():"-");
        map.put("state",ObjectUtils.isNotBlank(enterpriseFuWu.getState())?enterpriseFuWu.getState():"-");

        return coverMessage("200", "获取成功", map);
    }

    /**
     * 企业服务月    添加功能
     * @param enterpriseFuWu
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addenterprisefuwu",method = RequestMethod.POST)
    public Message addEnterpriseFuWu(@RequestBody EnterpriseFuWu enterpriseFuWu){
        service.save(enterpriseFuWu);
        return coverMessage("200", "添加成功");
    }

    /**
     * 企业服务月    修改功能
     * @param enterpriseFuWu   sysId(必须)
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/editenterprisefuwu",method = RequestMethod.POST)
    public Message editEnterpriseFuWu(@RequestBody EnterpriseFuWu enterpriseFuWu){
        EnterpriseFuWu newBean = service.getById(enterpriseFuWu.getSysId());
        if(ObjectUtils.isNotBlank(enterpriseFuWu.getFillDate())){
            newBean.setFillDate(enterpriseFuWu.getFillDate());
        }
        if(ObjectUtils.isNotBlank(enterpriseFuWu.getHelpEnterprise())){
            newBean.setHelpEnterprise(enterpriseFuWu.getHelpEnterprise());
        }
        if(ObjectUtils.isNotBlank(enterpriseFuWu.getFillUnitId())){
            newBean.setFillUnitId(enterpriseFuWu.getFillUnitId());
        }
        if(ObjectUtils.isNotBlank(enterpriseFuWu.getSpecificProblem())){
            newBean.setSpecificProblem(enterpriseFuWu.getSpecificProblem());
        }
        if(ObjectUtils.isNotBlank(enterpriseFuWu.getSolveSchedule())){
            newBean.setSolveSchedule(enterpriseFuWu.getSolveSchedule());
        }
        if(ObjectUtils.isNotBlank(enterpriseFuWu.getNextStep())){
            newBean.setNextStep(enterpriseFuWu.getNextStep());
        }
        if(ObjectUtils.isNotBlank(enterpriseFuWu.getRemark())){
            newBean.setRemark(enterpriseFuWu.getRemark());
        }
        if(ObjectUtils.isNotBlank(enterpriseFuWu.getState())){
            newBean.setState(enterpriseFuWu.getState());
        }
        service.update(newBean);
        return coverMessage("200", "修改成功");
    }

    /**
     * 企业服务月    删除功能
     * @param enterpriseFuWu
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteenterprisefuwu")
    public Message deleteEnterpriseFuWu(@RequestBody EnterpriseFuWu enterpriseFuWu){
        service.deleteById(enterpriseFuWu.getSysId());
        return coverMessage("200", "删除成功");
    }
}
